package com.bbva.gptf.back.dominio;

import java.math.BigDecimal;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bbva.jee.arq.spring.core.log.I18nLog;
import com.bbva.jee.arq.spring.core.log.I18nLogFactory;

@Component
@Configurable
public class SpGeneraOrdenesLayout {
	
	private final I18nLog log = I18nLogFactory.getLogI18n(SpGeneraOrdenesLayout.class);
			
	private SimpleJdbcCall simpleJdbcCall;
    private DataSource dataSource;

    @Autowired
    public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
    }

    @PersistenceContext
    transient EntityManager entityManager;
	
	@Transactional
    public Boolean execute(String ordenes, Integer registros, BigDecimal cdGestor, String cdUsuario){
    	String procedureName = "PKG_GPT.SP_GENERA_ORDENES_LAYOUT";
        this.simpleJdbcCall = new SimpleJdbcCall(this.dataSource);
        this.simpleJdbcCall.withoutProcedureColumnMetaDataAccess();
        this.simpleJdbcCall.withProcedureName(procedureName);
        this.simpleJdbcCall.declareParameters(new SqlParameter("p_ordenes", Types.CLOB));
        this.simpleJdbcCall.declareParameters(new SqlParameter("p_registros", Types.NUMERIC));
        this.simpleJdbcCall.declareParameters(new SqlParameter("p_cd_gestor", Types.NUMERIC));
        this.simpleJdbcCall.declareParameters(new SqlParameter("p_cd_usuario_umo", Types.VARCHAR));
        this.simpleJdbcCall.declareParameters(new SqlOutParameter("p_error", Types.VARCHAR));
        
        Map<String, Object> parametros = new HashMap<String, Object>();
        log.info("Metodo PKG_GPT.SP_GENERA_ORDENES_LAYOUT");
        log.info("Parametros: " + ordenes);
        log.info("Parametros: " + registros);
        log.info("Parametros: " + cdGestor);
        log.info("Parametros: " + cdUsuario);
        parametros.put("p_ordenes", ordenes);
        parametros.put("p_registros", registros);
        parametros.put("p_cd_gestor", cdGestor);
        parametros.put("p_cd_usuario_umo", cdUsuario);
        Map<String, Object> out = this.simpleJdbcCall.execute(parametros);
        String error = (String) out.get("p_error");
        return error != null ? false : true;
	}
}
